package indoor.nexd.com.nexdmap.util;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.nexd.map.rendering.core.componet.data.Feature;
import indoor.nexd.com.nexdmap.model.RecordBean;

/**
 * Created by lawrence on 2015/10/12.
 */
public class SharedPreferencesUtil {

    public static SharedPreferences getLastVisitMall(Context context) {
        return context.getSharedPreferences("last_visit_mall", Context.MODE_PRIVATE);
    }

    public static void setLastVisitMall(Context context, String cityName, String mallName, String floorName) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("last_visit_mall", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("city", cityName);
        edit.putString("mall", mallName);
        edit.putString("floorName", floorName);
        edit.apply();
    }


    public static void setAutoSignIn(Context context, int i) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("is_auto", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt("is_auto", i);
        editor.apply();
    }

    public static int isAutoSignIn(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("is_auto", Context.MODE_PRIVATE);
        return sharedPreferences.getInt("is_auto", 1);
    }

    public static void setSavePassword(Context context, String account, String password) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("save_password", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("account", account);
        editor.putString("password", password);
        editor.apply();
    }

    public static SharedPreferences getSavePassword(Context context) {
        return context.getSharedPreferences("save_password", Context.MODE_PRIVATE);
    }

    public static void putSearchRecord(Context context, String record, List<Feature> features) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("search_record", Context.MODE_PRIVATE);
        Set<String> featureSet = new HashSet<>();
        for (Feature feature : features) {
            featureSet.add(feature.getCenter_x() + "," + feature.getCenter_y());
        }
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putStringSet(record, featureSet).apply();
    }

    public static List<RecordBean> getSearchRecord(Context context) {
        Map<String, Set<String>> all = (Map<String, Set<String>>) context.getSharedPreferences("search_record", Context.MODE_PRIVATE).getAll();
        List<RecordBean> recordBeans = new ArrayList<>();
        Set<String> keySet = all.keySet();
        for (String key : keySet) {
            RecordBean recordBean = new RecordBean();
            recordBean.setRecord(key);
            recordBean.setPosition(all.get(key));
            recordBeans.add(recordBean);
        }
        return recordBeans;
    }

    public static SharedPreferences isFirstInstall(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("install_record", Context.MODE_PRIVATE);
        return sharedPreferences;
    }

}
